//
//  ProgressView.m
//  ProgressDemo
//
//  Created by Shanlin on 2017/3/2.
//  Copyright © 2017年 zhuxin. All rights reserved.
//

#import "ProgressView.h"

@interface ProgressView ()

@property(nonatomic,strong) CAShapeLayer *progressLayer;
@property(nonatomic,strong) CAShapeLayer *progressBGLayer;

@property (nonatomic,strong) UIImageView* locationImageView;//
@property (nonatomic,strong) UIImage* locationImage;//
@property (nonatomic,strong)  NSArray<UIColor*> *colors;//

@end

@implementation ProgressView

-(UIImage *)locationImage{
    if (!_locationImage) {
        _locationImage = [UIImage imageNamed:@"ecg_img_location"];
    }
    return _locationImage;
}

-(UIImageView *)locationImageView{
    if (!_locationImageView) {
        _locationImageView = [[UIImageView alloc] initWithImage:self.locationImage];
        _locationImageView.frame = CGRectMake(0, 0, 12, 15);
        _locationImageView.contentMode = UIViewContentModeScaleToFill;
    }
    return _locationImageView;
}

-(void)setProgress:(NSInteger)progress
{
    __weak typeof(self)weakSelf = self;
    if (progress <0) {
        progress = 0;
    }else if (progress >100){
        progress = 100;
    }
    _progress = progress;
    [weakSelf updateLocation];
}

- (instancetype)initWithFrame:(CGRect)frame  colors:(NSArray<UIColor*>*)colors;
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.colors = colors;
        [self createLayer];
        [self addSubview:self.locationImageView];
    }
    return self;
}


-(void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
}


-(void)createLayer{
    CAShapeLayer *layer = [CAShapeLayer layer];
    CGFloat progressHeight = self.frame.size.height - self.locationImageView.frame.size.height;
    layer.lineWidth = progressHeight;
    layer.fillColor = [UIColor greenColor].CGColor;
    layer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    CAShapeLayer *masklayer = [CAShapeLayer layer];
    masklayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    masklayer.lineCap = kCALineCapRound;
    masklayer.fillColor = [UIColor greenColor].CGColor;
    masklayer.strokeColor = [UIColor greenColor].CGColor;
    masklayer.strokeEnd = 1.0f;
    
//    CGFloat radius = progressHeight / 2;
    CGFloat radius = 0;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.locationImageView.frame.size.width / 2,self.locationImageView.frame.size.height, self.frame.size.width - self.locationImageView.frame.size.width, progressHeight) cornerRadius:radius];
    path.lineWidth = progressHeight;
    masklayer.path = path.CGPath;
    NSMutableArray* colors = [[NSMutableArray alloc] init];
    if (self.colors.count > 0) {
        for (UIColor *color in self.colors) {
            [colors addObject:(id)color.CGColor];
        }
    }else{
        [colors addObject:(id)[UIColor greenColor].CGColor];
    }
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    gradientLayer.colors = colors;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
//    gradientLayer.locations = @[@0.1f,@0.23f,@0.76f,@0.9f];
    
    [gradientLayer setMask:masklayer];
    
    [self.layer addSublayer:layer];
    [layer addSublayer:gradientLayer];
}

-(void)updateLocation{
    __weak typeof(self)weakSelf = self;
    CGFloat width = weakSelf.frame.size.width - weakSelf.locationImageView.frame.size.width;
    CGRect frame = weakSelf.locationImageView.frame;
    frame.origin.x = width * weakSelf.progress / 100;
    weakSelf.locationImageView.frame = frame;
}

@end
